package com.jamie;

import com.jamie.model.EnumInfoDefinition;
import com.jamie.utils.FreeMarkerTemplateUtils;
import freemarker.template.Template;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 枚举类代码生成工具
 * @author xuanweiyao
 */
public class EnumGenerateMain {

    /** 枚举需要生成到那个包下面 */
    private static final String PACKAGE_NAME = "com.jamie.test";

    /** 枚举类名称 */
    private static final String ENUM_CLASS_NAME = "BusinessTypeEnum";

    /** 枚举类生成后类上的注解 */
    private static final String ENUM_ANNOTATION = "配置业务培训";

    /** 枚举字段属性 */
    private static final String[][] ENUM_INFO = {
            {"0", "car"},
            {"1", "sf_car"}
    };

    /** 类上的作者 */
    private static final String AU_THOR = "xuanweiyao";

    /** 类上的日期 */
    private static final String CURRENT_DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

    public static void main(String[] args) throws Exception {
        //生成枚举类java文件
        generateEnumClassFile();
    }

    private static void generateEnumClassFile() throws Exception {
        final String suffix = "Enum.java";
        final String path = "E:\\jamie-product\\personal\\freemarker-example\\target\\" + ENUM_CLASS_NAME + suffix;
        final String templateName = "EnumClass.ftl";
        File mapperFile = new File(path);
        Map<String, Object> dataMap = new HashMap<>();
        List<EnumInfoDefinition> enumInfoDefinitionList = new ArrayList<>();
        EnumInfoDefinition enumInfoDefinition = null;
        for (int i = 0; i < ENUM_INFO.length; i++) {
            enumInfoDefinition = new EnumInfoDefinition();
            enumInfoDefinition.setEnumUuid(EnumGenerateMain.generateUUID());
            enumInfoDefinition.setEnumCode(ENUM_INFO[i][0]);
            enumInfoDefinition.setEnumName(ENUM_INFO[i][1]);
            enumInfoDefinition.setEnumNumber(i + 1 + "");
            enumInfoDefinition.setEnumSortOrder(i + 1 + "");
            enumInfoDefinitionList.add(enumInfoDefinition);
        }
        dataMap.put("enum_list", enumInfoDefinitionList);
        generateFileByTemplate(templateName, mapperFile, dataMap);
    }

    private static void generateFileByTemplate(final String templateName, File file, Map<String, Object> dataMap) throws Exception {
        File ftlDirPath = new File("E:\\jamie-product\\personal\\freemarker-example\\src\\main\\java\\com\\jamie\\ftl");
        // File ftlDirPath = new File(Objects.requireNonNull(Class.class.getResource("/")).getPath() + "\\ftl");
        Template template = FreeMarkerTemplateUtils.getTemplate(templateName, ftlDirPath);
        FileOutputStream fos = new FileOutputStream(file);
        dataMap.put("author", AU_THOR);
        dataMap.put("date", CURRENT_DATE);
        dataMap.put("package_name", PACKAGE_NAME);
        dataMap.put("enum_annotation", ENUM_ANNOTATION);
        dataMap.put("enum_class_name", ENUM_CLASS_NAME);
        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"), 10240);
        template.process(dataMap, out);
    }

    public static String generateUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

}